package cn.itcast.mq.bean.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.retry.ImmediateRequeueMessageRecoverer;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanRabbitConfig {

    /**
     * 简单队列模型
     */
    // 队列名
    public static final String SIMPLE_QUEUE = "simple.queue";

    // 创建队列
    @Bean
    public Queue simpleQueue() {
        return new Queue(SIMPLE_QUEUE);
    }

    /**
     * 重试次数耗尽后，重新入队
     *
     * @return
     */
//    @Bean
//    public MessageRecoverer messageRecoverer() {
//        return new ImmediateRequeueMessageRecoverer();
//    }

    /**
     * 工作队列模型
     */
    // 队列名称
    public static final String WORK_QUEUE = "work.queue";

    // 创建队列
    @Bean
    public Queue workQueue() {
        return new Queue(WORK_QUEUE);
    }


    /**
     * 广播模型
     */
    // 交换机名
    public static final String FANOUT_EXCHANGE = "fantout.exchange";
    // 队列1名
    public static final String FANOUT_QUEUE1 = "fanout.queue1";
    // 队列2名
    public static final String FANOUT_QUEUE2 = "fanout.queue2";

    // 创建交换机
    @Bean
    public FanoutExchange fanoutExchange() {
//        ExchangeBuilder.fanoutExchange(FANOUT_EXCHANGE).build();
        return new FanoutExchange(FANOUT_EXCHANGE);
    }

    // 创建队列1
    @Bean
    public Queue fanoutQueue1() {
//        QueueBuilder.durable(FANOUT_QUEUE1).lazy().build();
        return new Queue(FANOUT_QUEUE1);
    }

    // 创建队列2
    @Bean
    public Queue fanoutQueue2() {
        return new Queue(FANOUT_QUEUE2);
    }

    // 创建交换机与队列1的绑定关系
    @Bean
    public Binding bindingFanoutQueue1(Queue fanoutQueue1, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
    }

    // 创建交换机与队列2的绑定关系
    @Bean
    public Binding bindingFanoutQueue2(Queue fanoutQueue2, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
    }


    /**
     * 路由模型
     */
    // 交换机名
    public static final String DIRECT_EXCHANGE = "direct.exchange";

    // 路由KEY1名称
    public static final String DIRECT_ROUTEKEY_RED = "direct.red";
    // 路由KEY2名称
    public static final String DIRECT_ROUTEKEY_BLUE = "direct.blue";

    // 队列1名称
    public static final String DIRECT_QUEUE1 = "direct.queue1";
    // 队列2名称
    public static final String DIRECT_QUEUE2 = "direct.queue2";

    // 创建交换机
    @Bean
    public DirectExchange directExchange() {
//        return new DirectExchange(DIRECT_EXCHANGE);

        // 创建其对应的备份交换机
        return ExchangeBuilder.directExchange(DIRECT_EXCHANGE)
                .durable(true)
                .withArgument("alternate-exchange", BeanRabbitBackupConfig.BACKUP_EXCHANGE)
                .build();
    }

    // 创建队列1
    @Bean
    public Queue directQueue1() {
        return new Queue(DIRECT_QUEUE1);
    }

    // 创建队列2
    @Bean
    public Queue directQueue2() {
        return new Queue(DIRECT_QUEUE2);
    }

    // 绑定交换机与队列1，使用的路由KEY是RED
    @Bean
    public Binding bindingDirectQueue1(Queue directQueue1, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue1).to(directExchange).with(DIRECT_ROUTEKEY_RED);
    }

    // 绑定交换机与队列2，使用的路由KEY是BLUE
    @Bean
    public Binding bindingDirectQueue2(Queue directQueue2, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue2).to(directExchange).with(DIRECT_ROUTEKEY_BLUE);
    }


    /**
     * 主题模型
     */
    // 交换机名
    public static final String TOPIC_EXCHANGE = "topic.exchange";

    // 路由KEY1名称
    public static final String TOPIC_ROUTEKEY1 = "china.*";
    // 路由KEY2名称
    public static final String TOPIC_ROUTEKEY2 = "#.news";

    // 队列1名称
    public static final String TOPIC_QUEUE1 = "topic.queue1";
    // 队列2名称
    public static final String TOPIC_QUEUE2 = "topic.queue2";

    // 创建交换机
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE);
    }

    // 创建队列1
    @Bean
    public Queue topicQueue1() {
        return new Queue(TOPIC_QUEUE1);
    }

    // 创建队列2
    @Bean
    public Queue topicQueue2() {
        return new Queue(TOPIC_QUEUE2);
    }

    // 绑定交换机与队列1，使用通配符路由KEY1
    @Bean
    public Binding bindingTopicQueue1(Queue topicQueue1, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue1).to(topicExchange).with(TOPIC_ROUTEKEY1);
    }

    // 绑定交换机与队列2，使用通配符路由KEY2
    @Bean
    public Binding bindingTopicQueue2(Queue topicQueue2, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue2).to(topicExchange).with(TOPIC_ROUTEKEY2);
    }


    /**
     * 对象模型
     */
    // 队列名
    public static final String OBJECT_QUEUE = "object.queue";

    // 创建一个用于存储对象消息的队列
    @Bean
    public Queue objectQueue() {
        return new Queue(OBJECT_QUEUE);
    }

}
